script_enemy_main{

let type=GetCommonData("FamiliarType");

let shot1=0;
let bullet1=[];
let fade1=[];

let misangle=0;

let frame=0;
let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let SEshots7=("\script\SoundEffects\shots7.wav");
let GRfamiliar=("\script\Images\OtherEffects\SpellCircle1b.png");

@Initialize{
	LoadGraphic(GRfamiliar);

	SetScore(1);
	SetLife(1);

	if(type==1){ SetSpeed(2.3); }
	if(type==2){ SetSpeed(2); }
}
	
@MainLoop{

if(type==1){
	if(GetX<=minx && GetY<=miny){ SetAngle(0); }
	if(GetX>=maxx && GetY<=miny){ SetAngle(90); }
	if(GetX>=maxx && GetY>=maxy){ SetAngle(180); }
	if(GetX<=minx && GetY>=maxy){ SetAngle(270); }
}
if(type==2){
	if(GetX<=minx && GetY<=miny){ SetAngle(90); }
	if(GetX>=maxx && GetY<=miny){ SetAngle(180); }
	if(GetX>=maxx && GetY>=maxy){ SetAngle(270); }
	if(GetX<=minx && GetY>=maxy){ SetAngle(0); }
}


if(time%13==0 && time>=60){
let angle=time/3.1;
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetX,GetY);
	ObjShot_SetDelay(shot1,20);
	Obj_SetAngle(shot1,atan2(cy-Obj_GetY(shot1),cx-Obj_GetX(shot1))+rand(-misangle,misangle));
	Obj_SetSpeed(shot1,1.5);
		if(type==1){ ObjShot_SetGraphic(shot1,15); }
		if(type==2){ ObjShot_SetGraphic(shot1,35); }
	bullet1=bullet1~[shot1];
	fade1=fade1~[shot1];
	fade1[length(bullet1)-1]=0;
PlaySE(SEshots7);
misangle+=0.03;
}


let moonradius=GetCommonData("Radius");

if(time>=60){
let i=0;
	while(i<length(bullet1)){
		if(Obj_BeDeleted(bullet1[i])){
		bullet1=erase(bullet1,i); fade1=erase(fade1,i);
		i--;
		}
		else{
			if((((cx-Obj_GetX(bullet1[i]))^2+(cy-Obj_GetY(bullet1[i]))^2)^0.5)<moonradius){
			if(type==1){ Obj_SetAngle(bullet1[i],Obj_GetAngle(bullet1[i])-0.8); }
			if(type==2){ Obj_SetAngle(bullet1[i],Obj_GetAngle(bullet1[i])+0.8); }
			}
		}
	i++;
	}
}



time++;
frame++;

}

@DrawLoop{
	SetGraphicScale(0.2,0.2);
	SetTexture(GRfamiliar);
	SetRenderState(ADD);
		if(type==1){ SetColor(255,255,0); }
		if(type==2){ SetColor(255,255,255); }
	SetGraphicAngle(0,0,time*2);
	SetGraphicRect(0,0,200,200);
	DrawGraphic(GetX,GetY);
}

@Finalize{
}

}